package com.yzw.teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzw.common.domain.EduTeacher;
import com.yzw.common.dto.R;
import com.yzw.common.vo.SearchTeacherVo;
import com.yzw.common.vo.TeacherVo;
import com.yzw.teacher.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Yzw
 * @Description TODO
 * @Date 2023/5/1 12:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/teacher")
//@CrossOrigin
public class TeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping()
    public R selAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list();
        return R.success("查询所有讲师成功", list);
    }


    @ApiOperation(value = "新增讲师")
    @PostMapping
    public R save(@RequestBody @Validated TeacherVo teacherVo) {
        EduTeacher teacher = new EduTeacher();
        BeanUtils.copyProperties(teacherVo, teacher);
        eduTeacherService.save(teacher);
        return R.success("添加教师成功", null);
    }

    // 带分页查询 条件
    @ApiOperation(value = "分页查询")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public R page(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                  @RequestBody SearchTeacherVo searchTeacherVo) {
        Page<EduTeacher> list = eduTeacherService.getPage(pageNum, pageSize, searchTeacherVo);
        return R.success("查询所有讲师", list);
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        eduTeacherService.delete(id);
        return R.success("删除成功", null);
    }

    @ApiOperation(value = "id查询讲师成功")
    @GetMapping("/selById/{id}")
    public R selById(@PathVariable Long id) {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduTeacher::getId, id);
        EduTeacher teacher = eduTeacherService.list(queryWrapper).get(0);
        return R.success("id查询讲师成功", teacher);
    }

    @PutMapping
    public R updById(@RequestBody EduTeacher eduTeacher) {
        eduTeacherService.updateById(eduTeacher);
        return R.success("修改讲师成功", null);
    }
}
